package pixelReadWrite;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

/**
 * @author Andi
 */
public class ImagePixelReader {

	private BufferedImage img;
	private int rgb;
	private String pathToFile, pathToPic;
	private ArrayList<Pixel> way;
	private Pixel start;
	private Pixel goal;

	public ImagePixelReader(String pathToPic, String pathToFile) {
		this.way = new ArrayList<Pixel>();
		this.rgb = 0;
		this.pathToFile = pathToFile;
		this.pathToPic = pathToPic;
		try {
			this.img = ImageIO.read(new File(pathToPic));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void invoke() {
		System.out.println("1 %");
//		this.writeToFile(this.readPixelInfoFromPicInRgb());
		System.out.println("2 %");
		this.calcWayStartGoalByPic();
	}

	public BufferedImage getImg() {
		return img;
	}

	public Pixel getGoal() {
		return goal;
	}

	public Pixel getStart() {
		return start;
	}

	/**
	 * reads every pixel and returns the output with x,y and value (not rgb)
	 * 
	 * @return String with the Information for each pixel
	 */
	public String readPixelInfoFromPicInRgb() {
		// String stelle = ""; debugging
		String output = "";
		int red = 0;
		int green = 0;
		int blue = 0;

		for (int x = 0; x < img.getWidth(); x++) {
			for (int y = 0; y < img.getHeight(); y++) {
				System.out.println("(" + x + "/" + y + "),");
				rgb = img.getRGB(x, y);
				
				red = this.convertARGBtoRGB(rgb, "red");
				green = this.convertARGBtoRGB(rgb, "green");
				blue = this.convertARGBtoRGB(rgb, "blue");

				// output += this.convertARGBtoRGB(rgb, "red") + ","
				// + this.convertARGBtoRGB(rgb, "green") + ","
				// + this.convertARGBtoRGB(rgb, "blue") + "\n";
				// stelle = "(" + x + "/" + y + "),"; debugging
			}
		}

		return output;
	}

	public ArrayList<Pixel> getWay() {
		return this.way;
	}

	/**
	 * @param textToWrite
	 *            This text will be written in the File
	 * @param pathToFile
	 *            This File(path) will be written with the text described before
	 */
	public void writeToFile(String s) {
		try {
			// Create file
			FileWriter fstream = new FileWriter(this.pathToFile);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(s);
			// Close the output stream
			out.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	public void calcWayStartGoalByPic() {
		// BufferedReader buffered = null;
		// String[] splitted = new String[3];
		// String red = "";
		// String green = "";
		// String blue = "";
		// try {
		// buffered = new BufferedReader(new FileReader(this.pathToFile));
		// } catch (FileNotFoundException e) {
		// e.printStackTrace();
		// }
		int red = 0;
		int green = 0;
		int blue = 0;
		for (int x = 0; x < img.getWidth(); x++) {
			for (int y = 0; y < img.getHeight(); y++) {
				// try {
				// splitted = buffered.readLine().split(",");
				// } catch (IOException e) {
				// e.printStackTrace();
				// }
				// red = splitted[0];
				// green = splitted[1];
				// blue = splitted[2];
				rgb = img.getRGB(x, y);
				System.out.println(rgb);
				red = this.convertARGBtoRGB(rgb, "red");
				green = this.convertARGBtoRGB(rgb, "green");
				blue = this.convertARGBtoRGB(rgb, "blue");

				if (green + red + blue > 500) {
					continue;
				}
				if (green > 200) {
					this.start = new Pixel(x, y);
				} else if (red > 200) {
					this.goal = new Pixel(x, y);
				} else if (blue > 200) {
					this.way.add(new Pixel(x, y));
				}
			}
		}

	}

	public int getImgHeight() {
		return this.img.getHeight();
	}

	public int getImgWidth() {
		return this.img.getWidth();
	}

	public int convertARGBtoRGB(int argb, String color) {

		// int a = (argb >> 24) & 0xFF;

		if (color.equals("red"))
			return (argb >> 16) & 0xFF;
		if (color.equals("blue"))
			return (argb >> 0) & 0xFF;
		if (color.equals("green"))
			return (argb >> 8) & 0xFF;
		return -2999999;

	}
	// public static void main(String[] args) {
	// ImagePixelReader pp = new ImagePixelReader(
	// "C:\\Users\\Andi\\Desktop\\test.png",
	// "C:\\Users\\Andi\\Desktop\\outpt.txt");
	// System.out.println(pp.decimalToBinary(4557456));
	// }

}